//
//  MCSendHongBaoViewController.m
//  XDYL
//
//  Created by menhao on 2018/3/19.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCSendHongBaoViewController.h"
#import "MCHBTextFiled.h"
#import "MCSendToPersonViewController.h"
#import "MCHongBaoRecordViewController.h"
#import "MCSendHongBaoModel.h"
#import "MCPersonCountModel.h"
#import "MCUserMoneyModel.h"
#import "MCHasPayPwdModel.h"
#import "MCGetMaxRedCountModel.h"

@interface MCSendHongBaoViewController ()<UITextFieldDelegate,UIAlertViewDelegate>

@property (nonatomic,weak) MCHBTextFiled *totalMoneyTF;
@property (nonatomic,weak) MCHBTextFiled *hongBaoCountTF;
@property (nonatomic,weak) MCHBTextFiled *xiaoFeiTF;
@property (nonatomic,weak) MCHBTextFiled *chongZhiTF;
@property (nonatomic,weak) MCHBTextFiled *hongBaoNameTF;
@property (nonatomic,weak) UILabel *totalMoneyInfoLabel;
@property (nonatomic,weak) UILabel *sendNameLabel;
@property (nonatomic,strong) NSString *userId;
@property (nonatomic,weak) UIScrollView *baseScrollView;
@property (nonatomic,weak) UIButton *sendButton;
@property (nonatomic,strong) MCSendHongBaoModel *sendHongBaoModel;
@property (nonatomic,strong) MCPersonCountModel *personCountModel;
@property (nonatomic,assign) BOOL hongBaoType;
@property (nonatomic,assign) int type;
@property (nonatomic,strong) NSString *hongBaoMode;
@property (nonatomic,weak) UILabel *totalPersonInfoLabel;
@property (nonatomic,strong) MCUserMoneyModel * userMoneyModel;
@property (nonatomic,strong) MCGetMaxRedCountModel * getMaxRedCountModel;
@property (nonatomic,weak) UIAlertAction *sureAction;
@property (nonatomic,weak) UITextField *miMaTF;
@property (nonatomic,weak) UILabel *totalMoneyLabel;
@property (nonatomic,weak) UILabel *hongBaoNumInfoLabel;
@property (nonatomic,weak) UIView *xiaoFeiContentView;
@property (nonatomic,weak) UIView *hongBaoCountContentView;
@property (nonatomic,weak) UILabel *infoLabel;
@property (nonatomic,weak) UIButton *hongBaoButton;
@property (nonatomic,strong) MCSendToPersonViewController *vc;
@property (nonatomic,weak) UIView *sendToContentView;
@property (nonatomic,weak) UILabel *sendToLabel;
@property (nonatomic,weak) UIView *totalMoneyContentView;
@property (nonatomic,weak) UIView *hongBaoCountLabel;
@property (nonatomic,weak) UILabel *hongBaoCountUnitLabel;
@property (nonatomic,weak) UILabel *xiaoFeiUnitLabel;

@property (nonatomic,weak) UIView *chongZhiContentView;
@property (nonatomic,weak) UILabel *chongZhiLabel;
@property (nonatomic,weak) UILabel *xiaoFeiLabel;
@property (nonatomic,weak) UILabel *chongZhiUnitLabel;
@property (nonatomic,weak) UIView *hongBaoNameContentView;
@property (nonatomic,weak) UILabel *hongBaoNameLabel;
@property (nonatomic,assign) CGFloat heightHongBaoCountView;
@end

@implementation MCSendHongBaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self loadMaxRedCountData];
   
    
}



- (void)loadMaxRedCountData{
    MCGetMaxRedCountModel *getMaxRedCountModel = [[MCGetMaxRedCountModel alloc] init];
    self.getMaxRedCountModel = getMaxRedCountModel;
    [getMaxRedCountModel refreashDataAndShow];
    WS(wself);
    getMaxRedCountModel.callBackSuccessBlock = ^(ApiBaseManager *manager) {
        wself.infoLabel.text = [NSString stringWithFormat:@"每人每天最多发出%@个红包。\n未领取的红包，将于24小时后进行退款。",manager.ResponseRawData[@"DailyMaxRedEnvelopSendQty"]];;
        
    };
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.barTintColor = RGB(45,169,223);
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = RGB(255, 69, 69);
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.translucent = NO;

}
- (void)setUpUI{
    self.heightHongBaoCountView = MC_REALVALUE(40);
    UIScrollView *baseScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, G_SCREENWIDTH, G_SCREENHEIGHT - kStatusBarAndNavigationBarHeight)];
    [self.view addSubview:baseScrollView];
    baseScrollView.contentSize = CGSizeMake(G_SCREENWIDTH, G_SCREENHEIGHT);
    self.baseScrollView = baseScrollView;

    baseScrollView.showsVerticalScrollIndicator = NO;
    baseScrollView.showsHorizontalScrollIndicator = NO;
    CGFloat tempHeight = MC_REALVALUE(40);
    CGFloat tempWidth = MC_REALVALUE(105);
    CGFloat rightPadding = MC_REALVALUE(-15);
    CGFloat leftPadding = MC_REALVALUE(15);
    CGFloat leftInset = MC_REALVALUE(7);
    self.view.backgroundColor = RGB(238, 238, 238);
    self.navigationItem.title = @"发红包";
    self.navigationController.navigationBar.barTintColor = RGB(255, 69, 69);
    /*
     * 红包记录按钮
     */
    UIButton *btn = [[UIButton alloc] init];
    [btn setTitle:@"红包记录" forState:UIControlStateNormal];
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [btn sizeToFit];
    [btn addTarget:self action:@selector(hongBaoRecordBtnClick) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
    UIView *sendToContentView = [[UIView alloc] init];
    sendToContentView.backgroundColor = [UIColor whiteColor];
    sendToContentView.layer.cornerRadius = 6;
    sendToContentView.layer.masksToBounds = YES;
    [baseScrollView addSubview:sendToContentView];
    [sendToContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(leftPadding);
        make.right.equalTo(self.view.mas_right).offset(rightPadding);
        make.top.equalTo(baseScrollView).offset(MC_REALVALUE(29));
        make.height.equalTo(@(tempHeight));
    }];
    self.sendToContentView = sendToContentView;
    UILabel *sendToLabel = [[UILabel alloc] init];
    sendToLabel.text = @"发送对象：";
    sendToLabel.textColor =RGB(34, 34, 34);
    sendToLabel.textAlignment = NSTextAlignmentLeft;
    sendToLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [sendToContentView addSubview:sendToLabel];
    [sendToLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sendToContentView).offset(leftInset);
        make.centerY.equalTo(sendToContentView);
        make.width.equalTo(@(tempWidth));
        make.height.equalTo(@(tempHeight));
        
    }];
    self.sendToLabel = sendToLabel;
    UILabel *sendNameLabel = [[UILabel alloc] init];
    sendNameLabel.text = @"";
    sendNameLabel.textColor =RGB(45, 169, 223);
    sendNameLabel.textAlignment = NSTextAlignmentLeft;
    sendNameLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [sendToContentView addSubview:sendNameLabel];
    [sendNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sendToLabel.mas_right).offset(leftInset);
        make.centerY.equalTo(sendToContentView);
        
    }];
    self.sendNameLabel = sendNameLabel;
    UIButton *selectPersonButton = [[UIButton alloc] init];
    [sendToContentView addSubview:selectPersonButton];
    [selectPersonButton setTitle:@"选择 ＞" forState:UIControlStateNormal];
    selectPersonButton.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(13)];
    [selectPersonButton setTitleColor:RGB(45, 169, 223) forState:UIControlStateNormal];
    [selectPersonButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(sendToContentView);
        make.right.equalTo(sendToContentView.mas_right).offset(leftInset);
        make.height.equalTo(@(tempHeight));
        make.width.equalTo(@(MC_REALVALUE(90)));
    }];
    [selectPersonButton addTarget:self action:@selector(selectPersonButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *totalMoneyContentView = [[UIView alloc] init];
    totalMoneyContentView.backgroundColor = [UIColor whiteColor];
    totalMoneyContentView.layer.cornerRadius = 5;
    totalMoneyContentView.layer.masksToBounds = YES;
    self.totalMoneyContentView = totalMoneyContentView;
    [baseScrollView addSubview:totalMoneyContentView];
    [totalMoneyContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(sendToContentView);
        make.top.equalTo(sendToContentView.mas_bottom).offset(MC_REALVALUE(20));
        make.height.equalTo(sendToContentView.mas_height);
    }];
    
    UILabel *totalMoneyLabel = [[UILabel alloc] init];
    totalMoneyLabel.text = @"红包总金额：";
    totalMoneyLabel.textColor =RGB(34, 34, 34);
    totalMoneyLabel.textAlignment = NSTextAlignmentLeft;
    totalMoneyLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [totalMoneyContentView addSubview:totalMoneyLabel];
    [totalMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(totalMoneyContentView).offset(leftInset);
        make.centerY.equalTo(totalMoneyContentView);
        make.width.equalTo(@(tempWidth));
        make.height.equalTo(@(tempHeight));

    }];
    self.totalMoneyLabel = totalMoneyLabel;
    MCHBTextFiled *totalMoneyTF = [[MCHBTextFiled alloc] init];
    [totalMoneyContentView addSubview:totalMoneyTF];
    totalMoneyTF.textAlignment = NSTextAlignmentRight;
     totalMoneyTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"  可输入1~50000" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(13)],NSForegroundColorAttributeName:RGB(161, 161, 161)}];
    totalMoneyTF.keyboardType = UIKeyboardTypeDecimalPad;
    [totalMoneyTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(totalMoneyContentView);
        make.height.equalTo(@(tempHeight));
        make.left.equalTo(totalMoneyLabel.mas_right).offset(leftInset);
        make.right.equalTo(totalMoneyContentView.mas_right).offset(rightPadding - MC_REALVALUE(13));
    }];
    totalMoneyTF.delegate = self;
    self.totalMoneyTF = totalMoneyTF;
    UILabel *totalMoneyUnitLabel = [[UILabel alloc] init];
    totalMoneyUnitLabel.text = @"元";
    totalMoneyUnitLabel.textColor =RGB(34, 34, 34);
    totalMoneyUnitLabel.textAlignment = NSTextAlignmentLeft;
    totalMoneyUnitLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [totalMoneyContentView addSubview:totalMoneyUnitLabel];
    [totalMoneyUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(totalMoneyContentView.mas_right).offset(MC_REALVALUE(-13));
        make.centerY.equalTo(totalMoneyContentView);


    }];
    
    UILabel *totalMoneyInfoLabel = [[UILabel alloc] init];
    totalMoneyInfoLabel.text = @"当前为拼手气红包，改为";
    totalMoneyInfoLabel.textColor =RGB(161, 161, 161);
    totalMoneyInfoLabel.textAlignment = NSTextAlignmentLeft;
    totalMoneyInfoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    [baseScrollView addSubview:totalMoneyInfoLabel];
    [totalMoneyInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(totalMoneyContentView);
        make.top.equalTo(totalMoneyContentView.mas_bottom).offset(MC_REALVALUE(10));
    }];
    self.totalMoneyInfoLabel = totalMoneyInfoLabel;

    UIButton *hongBaoButton = [[UIButton alloc] init];
    [baseScrollView addSubview:hongBaoButton];
    hongBaoButton.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    [hongBaoButton setTitle:@"普通红包" forState:UIControlStateNormal];
    [hongBaoButton setTitle:@"拼手气红包" forState:UIControlStateSelected];
    [hongBaoButton setTitleColor:RGB(45, 169, 223) forState:UIControlStateNormal];
    [hongBaoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(totalMoneyInfoLabel);
        make.left.equalTo(totalMoneyInfoLabel.mas_right);
    }];
    [hongBaoButton addTarget:self action:@selector(normalHongBaoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.hongBaoButton = hongBaoButton;
    
    UIView *hongBaoCountContentView = [[UIView alloc] init];
    hongBaoCountContentView.backgroundColor = [UIColor whiteColor];
    hongBaoCountContentView.layer.cornerRadius = 5;
    hongBaoCountContentView.layer.masksToBounds = YES;
    [baseScrollView addSubview:hongBaoCountContentView];

    self.hongBaoCountContentView = hongBaoCountContentView;
    UILabel *hongBaoCountLabel = [[UILabel alloc] init];
    hongBaoCountLabel.text = @"红包个数：";
    hongBaoCountLabel.textColor =RGB(34, 34, 34);
    hongBaoCountLabel.textAlignment = NSTextAlignmentLeft;
    hongBaoCountLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [hongBaoCountContentView addSubview:hongBaoCountLabel];




    MCHBTextFiled *hongBaoCountTF = [[MCHBTextFiled alloc] init];
    [hongBaoCountContentView addSubview:hongBaoCountTF];
    hongBaoCountTF.textAlignment = NSTextAlignmentRight;
    hongBaoCountTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"  " attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(13)],NSForegroundColorAttributeName:RGB(161, 161, 161)}];

    hongBaoCountTF.delegate = self;
    hongBaoCountTF.keyboardType = UIKeyboardTypeNumberPad;
    hongBaoCountTF.autocorrectionType = UITextAutocorrectionTypeNo;
    self.hongBaoCountTF = hongBaoCountTF;
    UILabel *hongBaoCountUnitLabel = [[UILabel alloc] init];
    hongBaoCountUnitLabel.text = @"个";
    hongBaoCountUnitLabel.textColor =RGB(34, 34, 34);
    hongBaoCountUnitLabel.textAlignment = NSTextAlignmentLeft;
    hongBaoCountUnitLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [hongBaoCountContentView addSubview:hongBaoCountUnitLabel];

    self.hongBaoCountUnitLabel = hongBaoCountUnitLabel;
    
    UILabel *totalPersonInfoLabel = [[UILabel alloc] init];
    totalPersonInfoLabel.text = @"该分支共0人";
    totalPersonInfoLabel.textColor =RGB(161, 161, 161);
    totalPersonInfoLabel.textAlignment = NSTextAlignmentLeft;
    totalPersonInfoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    [baseScrollView addSubview:totalPersonInfoLabel];

    self.totalPersonInfoLabel = totalPersonInfoLabel;
    totalPersonInfoLabel.hidden = YES;

    UIView *xiaoFeiContentView = [[UIView alloc] init];
    xiaoFeiContentView.backgroundColor = [UIColor whiteColor];
    xiaoFeiContentView.layer.cornerRadius = 5;
    xiaoFeiContentView.layer.masksToBounds = YES;
    [baseScrollView addSubview:xiaoFeiContentView];

    self.xiaoFeiContentView = xiaoFeiContentView;
    UILabel *xiaoFeiLabel = [[UILabel alloc] init];
    xiaoFeiLabel.text = @"彩票消费要求：";
    xiaoFeiLabel.textColor =RGB(34, 34, 34);
    xiaoFeiLabel.textAlignment = NSTextAlignmentLeft;
    xiaoFeiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [xiaoFeiContentView addSubview:xiaoFeiLabel];

    MCHBTextFiled *xiaoFeiTF = [[MCHBTextFiled alloc] init];
    [xiaoFeiContentView addSubview:xiaoFeiTF];
    xiaoFeiTF.textAlignment = NSTextAlignmentRight;
    xiaoFeiTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"0" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(13)],NSForegroundColorAttributeName:RGB(161, 161, 161)}];

    
    xiaoFeiTF.keyboardType = UIKeyboardTypeDecimalPad;
    xiaoFeiTF.delegate = self;
    xiaoFeiTF.autocorrectionType = UITextAutocorrectionTypeNo;
    self.xiaoFeiTF = xiaoFeiTF;
    UILabel *xiaoFeiUnitLabel = [[UILabel alloc] init];
    xiaoFeiUnitLabel.text = @"元";
    xiaoFeiUnitLabel.textColor =RGB(34, 34, 34);
    xiaoFeiUnitLabel.textAlignment = NSTextAlignmentLeft;
    xiaoFeiUnitLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [baseScrollView addSubview:xiaoFeiUnitLabel];

    self.xiaoFeiUnitLabel = xiaoFeiUnitLabel;

    UIView *chongZhiContentView = [[UIView alloc] init];
    chongZhiContentView.backgroundColor = [UIColor whiteColor];
    chongZhiContentView.layer.cornerRadius = 5;
    chongZhiContentView.layer.masksToBounds = YES;
    [baseScrollView addSubview:chongZhiContentView];

    self.chongZhiContentView = chongZhiContentView;
    UILabel *chongZhiLabel = [[UILabel alloc] init];
    chongZhiLabel.text = @"充值要求：";
    chongZhiLabel.textColor =RGB(34, 34, 34);
    chongZhiLabel.textAlignment = NSTextAlignmentLeft;
    chongZhiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [chongZhiContentView addSubview:chongZhiLabel];

    self.chongZhiLabel = chongZhiLabel;


    MCHBTextFiled *chongZhiTF = [[MCHBTextFiled alloc] init];
    [chongZhiContentView addSubview:chongZhiTF];
    chongZhiTF.textAlignment = NSTextAlignmentRight;
    chongZhiTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"0" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(13)],NSForegroundColorAttributeName:RGB(161, 161, 161)}];

    chongZhiTF.delegate = self;
    chongZhiTF.keyboardType = UIKeyboardTypeDecimalPad;
    chongZhiTF.autocorrectionType = UITextAutocorrectionTypeNo;
    self.chongZhiTF = chongZhiTF;
    UILabel *chongZhiUnitLabel = [[UILabel alloc] init];
    chongZhiUnitLabel.text = @"元";
    chongZhiUnitLabel.textColor =RGB(34, 34, 34);
    chongZhiUnitLabel.textAlignment = NSTextAlignmentLeft;
    chongZhiUnitLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [chongZhiContentView addSubview:chongZhiUnitLabel];



    UIView *hongBaoNameContentView = [[UIView alloc] init];
    hongBaoNameContentView.backgroundColor = [UIColor whiteColor];
    hongBaoNameContentView.layer.cornerRadius = 5;
    hongBaoNameContentView.layer.masksToBounds = YES;
    [baseScrollView addSubview:hongBaoNameContentView];

    UILabel *hongBaoNameLabel = [[UILabel alloc] init];
    hongBaoNameLabel.text = @"红包名称：";
    hongBaoNameLabel.textColor =RGB(34, 34, 34);
    hongBaoNameLabel.textAlignment = NSTextAlignmentLeft;
    hongBaoNameLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [hongBaoNameContentView addSubview:hongBaoNameLabel];




    MCHBTextFiled *hongBaoNameTF = [[MCHBTextFiled alloc] init];
    [hongBaoNameContentView addSubview:hongBaoNameTF];
    hongBaoNameTF.textAlignment = NSTextAlignmentRight;
    hongBaoNameTF.delegate = self;
    hongBaoNameTF.autocorrectionType = UITextAutocorrectionTypeNo;
    hongBaoNameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"恭喜发财,大吉大利！" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(13)],NSForegroundColorAttributeName:RGB(161, 161, 161)}];

    [hongBaoNameTF addTarget:self action:@selector(textFieldEditing:) forControlEvents:UIControlEventEditingChanged];
    self.hongBaoNameTF = hongBaoNameTF;
    UIButton *sendButton = [[UIButton alloc] init];
    [baseScrollView addSubview:sendButton];
    [sendButton setTitle:@"发出0元" forState:UIControlStateNormal];
    sendButton.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sendButton.layer.cornerRadius = 6;
    sendButton.clipsToBounds = YES;
    sendButton.backgroundColor = RGB(255,69,69);
    self.sendButton = sendButton;

    [sendButton addTarget:self action:@selector(sendButtonClick:) forControlEvents:UIControlEventTouchUpInside];

//    UILabel *hongBaoNumInfoLabel = [[UILabel alloc] init];
//    hongBaoNumInfoLabel.text = @"对方可领取的红包金额为0.01-200元";
//    hongBaoNumInfoLabel.textColor =RGB(161, 161, 161);
//    hongBaoNumInfoLabel.textAlignment = NSTextAlignmentLeft;
//    hongBaoNumInfoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
//    [baseScrollView addSubview:hongBaoNumInfoLabel];
//    [hongBaoNumInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(sendButton);
//        make.top.equalTo(sendButton.mas_bottom).offset(MC_REALVALUE(10));
//    }];
//    self.hongBaoNumInfoLabel = hongBaoNumInfoLabel;

    UILabel *infoLabel = [[UILabel alloc] init];
    infoLabel.numberOfLines = 0;
    infoLabel.text = @"每人每天最多发出0个红包。\n未领取的红包，将于24小时后进行退款。";
    infoLabel.textColor =RGB(161, 161, 161);
    infoLabel.textAlignment = NSTextAlignmentCenter;
    infoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(10)];
    [baseScrollView addSubview:infoLabel];

    self.infoLabel = infoLabel;
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    [self.view addGestureRecognizer:singleTap];
    self.hongBaoMode = @"Lucky";

    if (@available(iOS 11.0, *)) {
        self.baseScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.hongBaoCountLabel = hongBaoCountLabel;
    self.hongBaoCountUnitLabel = hongBaoCountUnitLabel;
    self.chongZhiContentView = chongZhiContentView;
    self.chongZhiLabel = chongZhiLabel;
    self.xiaoFeiLabel = xiaoFeiLabel;
    self.chongZhiUnitLabel = chongZhiUnitLabel;
    self.hongBaoNameContentView = hongBaoNameContentView;
    self.hongBaoNameLabel = hongBaoNameLabel;

}

- (void)updateViewConstraints{
    

    CGFloat tempHeight = MC_REALVALUE(40);
    CGFloat tempWidth = MC_REALVALUE(105);
    CGFloat rightPadding = MC_REALVALUE(-15);
//    CGFloat leftPadding = MC_REALVALUE(15);
    CGFloat leftInset = MC_REALVALUE(7);
   
    [self.hongBaoCountContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.sendToContentView);
        make.top.equalTo(self.totalMoneyContentView.mas_bottom).offset(MC_REALVALUE(50));
        make.height.equalTo(@(self.heightHongBaoCountView));
        
    }];
   
    [self.hongBaoCountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.hongBaoCountContentView).offset(leftInset);
        make.centerY.equalTo(self.hongBaoCountContentView);
        make.width.equalTo(@(tempWidth));
        make.height.equalTo(self.hongBaoCountContentView.mas_height);
        
    }];
    
    
  
    [self.hongBaoCountTF mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hongBaoCountContentView);
        make.height.equalTo(@(tempHeight));
        make.left.equalTo(self.totalMoneyLabel.mas_right).offset(leftInset);
        make.right.equalTo(self.hongBaoCountContentView.mas_right).offset(rightPadding- MC_REALVALUE(13));
    }];
   
    [self.hongBaoCountUnitLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.hongBaoCountContentView.mas_right).offset(MC_REALVALUE(-13));
        make.centerY.equalTo(self.hongBaoCountContentView);
        
    }];
    
   
    [self.totalPersonInfoLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.totalMoneyContentView);
        make.top.equalTo(self.hongBaoCountContentView.mas_bottom).offset(MC_REALVALUE(10));
    }];
    if (self.heightHongBaoCountView == 0) {
        [self.xiaoFeiContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.sendToContentView);
            make.top.equalTo(self.totalMoneyContentView.mas_bottom).offset(MC_REALVALUE(13));
            make.height.equalTo(self.sendToContentView.mas_height);
        }];
    } else {
        [self.xiaoFeiContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.sendToContentView);
            make.top.equalTo(self.hongBaoCountContentView.mas_bottom).offset(MC_REALVALUE(50));
            make.height.equalTo(self.sendToContentView.mas_height);
        }];
    }

   
    [self.xiaoFeiLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.xiaoFeiContentView).offset(leftInset);
        make.centerY.equalTo(self.xiaoFeiContentView);
        make.width.equalTo(@(tempWidth));
        make.height.equalTo(@(tempHeight));
        
    }];
   
    [self.xiaoFeiTF mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.xiaoFeiContentView);
        make.height.equalTo(@(tempHeight));
        make.left.equalTo(self.totalMoneyLabel.mas_right).offset(leftInset);
        make.right.equalTo(self.xiaoFeiContentView.mas_right).offset(rightPadding- MC_REALVALUE(13));
    }];
  
    
    [self.xiaoFeiUnitLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.xiaoFeiContentView.mas_right).offset(MC_REALVALUE(-13));
        make.centerY.equalTo(self.xiaoFeiContentView);
    }];
    
    
   
    [self.chongZhiContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.sendToContentView);
        make.top.equalTo(self.xiaoFeiContentView.mas_bottom).offset(MC_REALVALUE(10));
        make.height.equalTo(self.sendToContentView.mas_height);
        
    }];
    
    
    [self.chongZhiLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.chongZhiContentView).offset(leftInset);
        make.centerY.equalTo(self.chongZhiContentView);
        make.width.equalTo(@(tempWidth));
        make.height.equalTo(@(tempHeight));
        
    }];
    
    

    [self.chongZhiTF mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.chongZhiContentView);
        make.height.equalTo(@(tempHeight));
        make.left.equalTo(self.chongZhiLabel.mas_right).offset(leftInset);
        make.right.equalTo(self.chongZhiContentView.mas_right).offset(rightPadding- MC_REALVALUE(13));
    }];

  
    [self.chongZhiUnitLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.chongZhiContentView.mas_right).offset(MC_REALVALUE(-13));
        make.centerY.equalTo(self.chongZhiContentView);
    }];
    

    [self.hongBaoNameContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.sendToContentView);
        make.top.equalTo(self.chongZhiContentView.mas_bottom).offset(MC_REALVALUE(10));
        make.height.equalTo(self.sendToContentView.mas_height);
        
    }];
    

    [self.hongBaoNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.hongBaoNameContentView).offset(leftInset);
        make.centerY.equalTo(self.hongBaoNameContentView);
        make.width.equalTo(@(tempWidth));
        make.height.equalTo(@(tempHeight));
        
    }];
    
    

    [self.hongBaoNameTF mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hongBaoNameContentView);
        make.height.equalTo(@(tempHeight));
        make.left.equalTo(self.chongZhiLabel.mas_right).offset(leftInset);
        make.right.equalTo(self.hongBaoNameContentView.mas_right).offset(MC_REALVALUE(-13));
    }];
  
    [self.sendButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hongBaoNameContentView.mas_bottom).offset(MC_REALVALUE(37));
        make.height.equalTo(@(tempHeight));
        make.left.right.equalTo(self.sendToContentView);
    }];
   
  
    [self.infoLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.sendButton.mas_bottom).offset(MC_REALVALUE(72));
    }];
 

    [super updateViewConstraints];
}

-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer{
    [self.view endEditing:YES];
}
- (void)textFieldEditing:(UITextField *)tf{
    if (tf.text.length > 30) {
        tf.text = [tf.text substringWithRange:NSMakeRange(0, 30)];
        [SVProgressHUD showErrorWithStatus:@"红包名称为30个字符以内,长度超过的已经为您截取。"];
    }
}
//点击消费要求
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.xiaoFeiTF) {
        [self.baseScrollView setContentOffset:CGPointMake(0, 60) animated:YES];
    } else if(textField == self.totalMoneyTF) {
        [self.baseScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else if(textField == self.hongBaoNameTF){
        [self.baseScrollView setContentOffset:CGPointMake(0, 60) animated:YES];
    }else{}
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    // 处理小数点
    if ([textField.text containsString:@"."]) {
        NSArray *a = [textField.text componentsSeparatedByString:@"."];
        if (a.count ==2) {
            NSString *s = a.lastObject;
            if(s.length >= 2){
                textField.text = [NSString stringWithFormat:@"%@.%@",a.firstObject,[s substringWithRange:NSMakeRange(0, 2)]];
            }else if (s.length == 1){
                textField.text = [NSString stringWithFormat:@"%@.%@0",a.firstObject,[s substringWithRange:NSMakeRange(0, 1)]];
            }
            if ([s isEqualToString:@""]) {
            
                    textField.text = [NSString stringWithFormat:@"%@.00",a.firstObject];
                
            }
            
        }
    }
//    过大处理
    if(textField == self.totalMoneyTF ) {
        if (self.hongBaoType == NO) {
            if ([textField.text doubleValue] >= 50000) {
                self.totalMoneyTF.text = @"50000";
            }
            if ([textField.text doubleValue] <= 1) {
                self.totalMoneyTF.text = @"1";
            }
        } else {
            if ([textField.text doubleValue] >= 50000) {
                self.totalMoneyTF.text = @"50000";
            }
            if ([textField.text doubleValue] <= 1) {
                self.totalMoneyTF.text = @"1";
            }
        }
        
 
        
    }else if(textField == self.hongBaoCountTF){
        
//            if ([textField.text doubleValue] >= 500) {
//                self.hongBaoCountTF.text = @"500";
//            }
            if ([textField.text doubleValue] <= 1) {
                self.hongBaoCountTF.text = @"1";
            }
        
     
    }else{}
    
    
    if ([self.hongBaoMode isEqualToString:@"Lucky"]) {
        if ([self.totalMoneyTF.text doubleValue]/[self.hongBaoCountTF.text doubleValue] < 0.01) {
            [SVProgressHUD showErrorWithStatus:@"单个红包不能低于0.01元"];
            return;
        }
    }
    NSString *text = @"";
    if (self.hongBaoType == NO) {
        text = self.totalMoneyTF.text;
    } else {
        double f = [self.totalMoneyTF.text doubleValue] * [self.hongBaoCountTF.text intValue];
        text = GetRealFloatNum(f);
    }
    if ([text isEqualToString:@""] || text == nil) {
        text = @"0";
    }
    // 处理文字颜色
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"发出%@元",text]];
    NSArray *number = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    for (int i = 0; i < str.length; i ++) {
        NSString *a = [[NSString stringWithFormat:@"发出%@元",text] substringWithRange:NSMakeRange(i, 1)];
        if ([number containsObject:a]) {
            [str addAttributes:@{NSForegroundColorAttributeName:[UIColor yellowColor],NSFontAttributeName:[UIFont systemFontOfSize:MC_REALVALUE(14)]} range:NSMakeRange(i, 1)];
        }
    }
 
    [self.sendButton setAttributedTitle: str forState:UIControlStateNormal];

}
//选择对象点击
- (void)selectPersonButtonClick{
    MCSendToPersonViewController *vc = [[MCSendToPersonViewController alloc] init];
    __weak __typeof(self)wself = self;
    self.vc = vc;
    MCPersonCountModel *personCountModel = [[MCPersonCountModel alloc]init];
    self.personCountModel = personCountModel;
    

    vc.selectedPersonBlock = ^(MCPersonHongBaoModel *p,int type) {
        wself.totalMoneyInfoLabel.hidden = NO;
        wself.hongBaoButton.hidden = NO;
        NSString *nameStr = @"";
//         wself.hongBaoCountContentView.hidden = NO;
         wself.heightHongBaoCountView = MC_REALVALUE(40);
        if (type == 1) {
            nameStr = [NSString stringWithFormat:@"%@的整个团队",p.ChildUserName];
        } else if (type == 2){
            nameStr = [NSString stringWithFormat:@"%@的直属下级",p.ChildUserName];
        }else{
            nameStr = p.ChildUserName;
            wself.totalMoneyInfoLabel.hidden = YES;
            wself.hongBaoButton.hidden = YES;
//            wself.hongBaoCountContentView.hidden = YES;
             wself.heightHongBaoCountView = MC_REALVALUE(0);
        }
         [self.view setNeedsUpdateConstraints];
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:nameStr];
        [attributeString addAttributes:@{NSForegroundColorAttributeName:RGB(45, 169, 223)} range:NSMakeRange(0, nameStr.length)];
        [attributeString addAttributes:@{NSForegroundColorAttributeName:RGB(34, 34, 34)} range:NSMakeRange(p.ChildUserName.length, nameStr.length - p.ChildUserName.length)];
        
        wself.sendNameLabel.attributedText = attributeString;
        wself.userId = [NSString stringWithFormat:@"%d",p.User_ID];
        wself.type = type;
        personCountModel.objectID = type;
        personCountModel.UserID = p.User_ID;
        if (wself.type == 3) {
            wself.hongBaoCountTF.text = @"1";
            wself.hongBaoCountTF.userInteractionEnabled =  NO;
            wself.totalPersonInfoLabel.hidden = YES;

        } else {
            wself.hongBaoCountTF.userInteractionEnabled =  YES;
            wself.totalPersonInfoLabel.hidden = NO;
//            wself.hongBaoMode = @"Balance";
        }

        [personCountModel refreashDataAndShow];
        __weak __typeof(self)wself = self;
        personCountModel.callBackSuccessBlock = ^(ApiBaseManager *manager) {
            
            wself.totalPersonInfoLabel.text = [NSString stringWithFormat:@"该分支共%@人",manager];
        };
        personCountModel.callBackFailedBlock = ^(id manager, NSDictionary *errorCode) {

        };
    };
    [self.navigationController pushViewController:vc animated:YES];
}

//普通红包点击
- (void)normalHongBaoButtonClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    NSString *text = @"";
 
    
   
    if (btn.selected) {
        self.hongBaoMode = @"Balance";
        self.hongBaoType = YES;
        self.totalMoneyInfoLabel.text = @"当前为普通红包，改为";
      
        self.totalMoneyTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"  可输入1~50000" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(13)],NSForegroundColorAttributeName:RGB(161, 161, 161)}];
        text = self.totalMoneyTF.text;
        self.totalMoneyLabel.text = @"单个红包金额:";
//        self.hongBaoNumInfoLabel.hidden = YES;

    } else {
        self.hongBaoMode = @"Lucky";
         self.hongBaoType = NO;
       self.totalMoneyInfoLabel.text = @"当前为拼手气红包，改为";
        self.totalMoneyTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"  可输入1~50000" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:MC_REALVALUE(13)],NSForegroundColorAttributeName:RGB(161, 161, 161)}];
        
        self.totalMoneyLabel.text = @"红包总金额:";
//        self.hongBaoNumInfoLabel.hidden = NO;

    }
    self.totalMoneyTF.text = @"";
    self.hongBaoCountTF.text = @"";
    if (self.type == 3) {
        self.hongBaoCountTF.text = @"1";
    }
}

//发送红包点击
- (void)sendButtonClick:(UIButton *)btn{
    

    if ([self.sendNameLabel.text isEqualToString:@""]||self.sendNameLabel.text == nil) {
        [SVProgressHUD showErrorWithStatus:@"发送对象不能为空！"];
        return;
    }
    if ([self.totalMoneyTF.text isEqualToString:@""]||self.totalMoneyTF.text == nil) {
        [SVProgressHUD showErrorWithStatus:@"红包金额不能为空！"];
        return;
    }
    if ([self.hongBaoCountTF.text isEqualToString:@""]||self.hongBaoCountTF.text == nil) {
        [SVProgressHUD showErrorWithStatus:@"红包个数不能为空！"];
        return;
    }
    if ([self.xiaoFeiTF.text isEqualToString:@""]||self.xiaoFeiTF.text == nil) {
        self.xiaoFeiTF.text = @"0";
    }
    if ([self.chongZhiTF.text isEqualToString:@""]||self.chongZhiTF.text == nil) {
        self.chongZhiTF.text = @"0";
    }
    if ([self.xiaoFeiTF.text doubleValue] < 0 ||[self.xiaoFeiTF.text doubleValue] > 999999999 ) {
    
        [SVProgressHUD showErrorWithStatus:@"彩票消费要求：0-999999999元"];
        return;
    }
       if ([self.chongZhiTF.text doubleValue] < 0 ||[self.chongZhiTF.text doubleValue] > 999999999 ) {
        
        [SVProgressHUD showErrorWithStatus:@"彩票充值要求：0-999999999元"];
        return;
    }

    MCSendHongBaoModel *sendHongBaoModel = [[MCSendHongBaoModel alloc]init];
    self.sendHongBaoModel = sendHongBaoModel;
    if ([self.hongBaoNameTF.text isEqualToString:@""] || self.hongBaoNameTF.text == nil) {
        sendHongBaoModel.Name = @"恭喜发财,大吉大利！";
    }else{
    sendHongBaoModel.Name = [self.hongBaoNameTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        if ([sendHongBaoModel.Name isEqualToString:@""]) {
             sendHongBaoModel.Name = @"恭喜发财,大吉大利！";
        } 
    }
    NSString *t = @"";
    if (self.type == 1) {
        t = @"Descendant";
    } else if(self.type == 2){
        t = @"Children";
    }else{
        t = @"SinglePerson";
        self.hongBaoMode = @"Balance";
    }
    sendHongBaoModel.ReceiveUserCategory = t;
    sendHongBaoModel.OperandUserId = [self.userId intValue];//@"4";
    sendHongBaoModel.Amount = [self.totalMoneyTF.text intValue];
    sendHongBaoModel.Mode = self.hongBaoMode;//self.type
    sendHongBaoModel.Qty = [self.hongBaoCountTF.text intValue];
    sendHongBaoModel.Person_Consumption = [self.xiaoFeiTF.text doubleValue];
    sendHongBaoModel.Person_Recharge_Money = [self.chongZhiTF.text doubleValue];
    
    if ([self.xiaoFeiTF.text isEqualToString:@""]||self.totalMoneyTF.text == nil) {

        sendHongBaoModel.Person_Consumption = 0;
    }
    if ([self.chongZhiTF.text isEqualToString:@""]||self.totalMoneyTF.text == nil) {

        sendHongBaoModel.Person_Recharge_Money = 0;
    }
    MCHasPayPwdModel * model = [MCHasPayPwdModel sharedMCHasPayPwdModel];
    BOOL hasPassword = [model.PayOutPassWord intValue]==1?YES:NO;
    //有资金密码
    if(hasPassword){
        
         [self setUpPop];
        //没有资金密码
    }else{
        
        [SVProgressHUD showInfoWithStatus:@"请先设置资金密码！"];
        
    }
   
    __weak __typeof(self)wself = self;
    sendHongBaoModel.callBackSuccessBlock = ^(ApiBaseManager *manager) {
      
        wself.hongBaoCountTF.text = @"";
        wself.totalMoneyTF.text = @"";
        wself.sendNameLabel.text = @"";
        wself.xiaoFeiTF.text = @"";
        wself.chongZhiTF.text = @"";
        wself.hongBaoNameTF.text = @"";
    
        [wself.sendButton setAttributedTitle: [[NSAttributedString alloc] initWithString:@"发出0元"] forState:UIControlStateNormal];
        [SVProgressHUD showSuccessWithStatus:@"发送成功！"];
        
       [MBProgressHUD hideHUDForView:self.view animated:YES];
//        [wself performSelector:@selector(pop2TopVC) withObject:nil afterDelay:2];
    };
    sendHongBaoModel.callBackFailedBlock = ^(ApiBaseManager * manager, NSDictionary *errorCode) {
       [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD showErrorWithStatus:manager.responseMessage];
    };
}
- (void)pop2TopVC{
     [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpPop{
    __weak __typeof__ (self) wself = self;

    //创建UIAlertController 设置标题，信息，样式
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请输入资金密码" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    //添加UITextField
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入资金密码！";
        textField.keyboardType = UIKeyboardTypeDefault;
        [textField addTarget:wself action:@selector(textFieldChane:) forControlEvents:UIControlEventEditingChanged];
        textField.secureTextEntry = YES;
        wself.miMaTF = textField;
    }];
    
    //创建UIAlertAction对象，设置标题并添加到UIAlertController上
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
      
       
        
    }];
    [alertController addAction:cancelAction];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
       
        wself.sendHongBaoModel.PayPassWord = self.miMaTF.text;
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [wself.sendHongBaoModel refreashDataAndShow];
        
    }];
    sureAction.enabled=NO;
    self.sureAction = sureAction;
    //设置UIAlertAction对象是否可用
    [alertController addAction:sureAction];
    
    //展现UIAlertController
    [self presentViewController:alertController animated:YES completion:nil];
}
-(void)textFieldChane:(UITextField *)tf{
    NSString *str = [tf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (str.length>=6) {
        _sureAction.enabled=YES;
//        self.sendButton.enabled = NO;
    }else{
//        self.sendButton.enabled = YES;
        _sureAction.enabled=NO;
    }
}
//红包记录点击
- (void)hongBaoRecordBtnClick{
    MCHongBaoRecordViewController * Vc =[[MCHongBaoRecordViewController alloc]init];
    [self.navigationController pushViewController:Vc animated:YES];
}


@end
